<template>
  <div class="tactic-item" @click="tacticDetails" v-if="!search || searchName">
    <template v-if="elShow || searchName">
      <div class="info">
        <div class="name">
          <h3>
            <i class="tactic-name" v-html="searchName || tactic.name"></i>
            <span :type="tactic.qualityName">{{ tactic.qualityName }}</span>
          </h3>
          <div class="tags">
            <div class="tag">{{ tactic.typeName }}</div>
            <div class="tag" v-if="tactic.effect_type">
              {{ tactic.effectTypeName }}
            </div>
          </div>
        </div>
        <div class="desc">
          <div class="odds">发动率：{{ tactic.probability }}%</div>
          <div class="target">目标：{{ tactic.target }}</div>
        </div>
      </div>
      <div
        class="practice"
        :class="{ active: practice }"
        @click.stop="practiceTactic"
      >
        <template v-if="practice">
          <span>已</span><span>演</span><span>练</span>
        </template>
        <template v-else>
          <span>未</span><span>演</span><span>练</span></template
        >
      </div>
    </template>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  props: {
    tactic: Object,
    search: String,
  },
  name: "TacticItem",
  methods: {
    tacticDetails() {
      this.$router.push("/tactic/" + this.tactic.id);
    },
    practiceTactic() {
      this.$store.dispatch("data/setUserTactics", this.tactic.id);
    },
    updateView() {
      if (!this.$el.getBoundingClientRect) return;
      let offsetTop = this.$el.getBoundingClientRect().top;
      if (offsetTop < this.viewHeight && offsetTop > -100) {
        this.elShow = true;
      } else {
        this.elShow = false;
      }
    },
  },
  data() {
    return {
      elShow: false,
    };
  },
  computed: {
    ...mapState("data", ["userTactics"]),
    searchName() {
      if (this.search.trim()) {
        if (this.tactic.name.indexOf(this.search.trim()) !== -1) {
          var reg = new RegExp(this.search.trim(), "g");
          let name = this.tactic.name.replace(
            reg,
            "<b>" + this.search.trim() + "</b>"
          );
          return name;
        } else {
          return "";
        }
      } else {
        return "";
      }
    },
    practice() {
      let practice = this.userTactics.indexOf(this.tactic.id) !== -1;
      return practice;
    },
  },
  mounted() {
    this.updateView();
    this.event.on("handleScroll", this.updateView);
  },
  beforeDestroy() {
    this.event.remove("handleScroll", this.updateView);
  },
};
</script>

<style lang="scss" scoped>
.tactic-item {
  height: 0.64rem;
  background: #ffffff;
  border-radius: 4px;
  border: 1px solid #ebebeb;
  margin-bottom: 0.09rem;
  display: flex;
  align-items: center;
  padding: 0 0.08rem;
  .info {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    height: 0.46rem;
    padding: 0 0.04rem 0 0;
    .name {
      display: flex;
      align-items: center;
      h3 {
        margin: 0;
        min-width: 0.9rem;
        height: 0.24rem;
        background: #f2f2f2;
        border-radius: 4px;
        font-weight: 400;
        font-size: 0.14rem;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0 0.02rem;
        .tactic-name {
          font-style: normal;
          ::v-deep {
            b {
              color: #f6a16e;
              font-weight: 400;
            }
          }
        }
        span {
          margin-left: 0.03rem;
          width: 0.16rem;
          height: 0.16rem;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          background: #f6a16e;
          border-radius: 50%;
          color: #fff;
          font-size: 0.12rem;
          &[type="S"] {
            background: #f6a16e;
          }
          &[type="A"] {
            background: #c483c0;
          }
          &[type="B"] {
            background: #81a0cf;
          }
          &[type="C"] {
            background: #96bfa3;
          }
        }
      }
      .tags {
        margin-left: 0.14rem;
        display: flex;
        align-items: center;
        .tag {
          display: flex;
          align-items: center;
          justify-content: center;
          min-width: 0.48rem;
          height: 0.2rem;
          padding: 0 0.05rem;
          box-sizing: border-box;
          background: #ffffff;
          border-radius: 0.1rem;
          border: 1px solid #ebebeb;
          font-size: 0.12rem;
          color: #333333;
          margin-right: 0.04rem;
        }
      }
    }
    .desc {
      display: flex;
      align-items: center;
      .odds {
        width: 0.81rem;
        font-size: 0.12rem;
        font-weight: 400;
        color: #8c8c8c;
        margin-left: 0.05rem;
        margin-right: 0.18rem;
        white-space: nowrap;
      }
      .target {
        font-size: 0.12rem;
        font-weight: 400;
        color: #8c8c8c;
      }
    }
  }
  .practice {
    width: 0.26rem;
    height: 0.48rem;
    padding: 0 0.05rem;
    background: #ffffff;
    border-radius: 4px;
    border: 1px solid #ebebeb;
    text-align: center;
    color: #8c8c8c;
    box-sizing: border-box;
    font-size: 0.12rem;
    line-height: 0.14rem;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
    &.active {
      background: #ff8a69;
      border: 1px solid #ff8a69;
      color: #fff;
    }
  }
}
</style>
